package com.newtype.meeting.client.widget.shared;

import java.util.List;
import java.util.Map;

import com.extjs.gxt.ui.client.Style.LayoutRegion;
import com.extjs.gxt.ui.client.Style.Scroll;
import com.extjs.gxt.ui.client.data.ModelData;
import com.extjs.gxt.ui.client.data.ModelIconProvider;
import com.extjs.gxt.ui.client.event.ButtonEvent;
import com.extjs.gxt.ui.client.event.CheckChangedEvent;
import com.extjs.gxt.ui.client.event.CheckChangedListener;
import com.extjs.gxt.ui.client.event.SelectionListener;
import com.extjs.gxt.ui.client.store.ListStore;
import com.extjs.gxt.ui.client.store.Store;
import com.extjs.gxt.ui.client.store.TreeStore;
import com.extjs.gxt.ui.client.util.Format;
import com.extjs.gxt.ui.client.util.IconHelper;
import com.extjs.gxt.ui.client.util.Margins;
import com.extjs.gxt.ui.client.widget.ContentPanel;
import com.extjs.gxt.ui.client.widget.Html;
import com.extjs.gxt.ui.client.widget.ListView;
import com.extjs.gxt.ui.client.widget.VerticalPanel;
import com.extjs.gxt.ui.client.widget.Window;
import com.extjs.gxt.ui.client.widget.button.Button;
import com.extjs.gxt.ui.client.widget.button.ButtonBar;
import com.extjs.gxt.ui.client.widget.form.Field;
import com.extjs.gxt.ui.client.widget.form.StoreFilterField;
import com.extjs.gxt.ui.client.widget.layout.BorderLayout;
import com.extjs.gxt.ui.client.widget.layout.BorderLayoutData;
import com.extjs.gxt.ui.client.widget.layout.FormLayout;
import com.extjs.gxt.ui.client.widget.treepanel.TreePanel;
import com.extjs.gxt.ui.client.widget.treepanel.TreePanel.CheckCascade;
import com.google.gwt.core.client.GWT;
import com.google.gwt.user.client.ui.AbstractImagePrototype;

/**
 * 组织选择（选取部门继承至子部门）
 * @author 汪恭泽
 *	
 */
public class OrganizationMultiSeperateSelectWindow extends Window {
	private TreeStore<ModelData> store = new TreeStore<ModelData>();
	private ListStore<ModelData> deptStore = new ListStore<ModelData>();//部门
	private ListStore<ModelData> staffStore = new ListStore<ModelData>();//人员
	private ListView<ModelData> listView1;
	private ListView<ModelData> listView2;

	public OrganizationMultiSeperateSelectWindow(
			Map<String, Field<String>> organization) {
		setSize(450, 500);
		setModal(true);
		setLayout(new BorderLayout());

		createWest();
		createCenter();
		addButton(getConfirmButton(organization));
		addButton(getCancelButton());
	}

	/**
	 * 显示已选中的部门和人员
	 */
	private void createCenter() {
		BorderLayoutData centerData = new BorderLayoutData(LayoutRegion.CENTER);
		centerData.setMargins(new Margins(5));
		ContentPanel centerPanel = new ContentPanel();
		centerPanel.setHeaderVisible(false);
		centerPanel.setLayout(new BorderLayout());
		centerPanel.setBodyBorder(false);

		//显示所选部门
		BorderLayoutData aboveData = new BorderLayoutData(LayoutRegion.NORTH,
				200);
		aboveData.setCollapsible(true);
		aboveData.setFloatable(true);
		aboveData.setMargins(new Margins(0, 0, 5, 0));

		VerticalPanel deptPanel = new VerticalPanel();
		deptPanel.setBorders(true);
		deptPanel.setLayout(new FormLayout());
		deptPanel.setScrollMode(Scroll.AUTO);
		deptPanel.setStyleAttribute("backgroundColor", "white");

		listView1 = new ListView<ModelData>() {
			@Override
			protected ModelData prepareData(ModelData model) {
				if (model.get("accountID") == null) {
					String name = model.get("name");
					model.set("shortName", Format.ellipse(name, 15));
					return model;
				}
				return null;
			}
		};
		listView1.setStore(deptStore);
		listView1.setDisplayProperty("shortName");
		listView1.setBorders(false);

		//显示人员
		BorderLayoutData belowData = new BorderLayoutData(LayoutRegion.CENTER);
		belowData.setMargins(new Margins(0));

		VerticalPanel staffPanel = new VerticalPanel();
		staffPanel.setBorders(true);
		staffPanel.setLayout(new FormLayout());
		staffPanel.setScrollMode(Scroll.AUTO);
		staffPanel.setStyleAttribute("backgroundColor", "white");

		listView2 = new ListView<ModelData>() {
			@Override
			protected ModelData prepareData(ModelData model) {
				if (model.get("accountID") != null) {
					String fullName = model.get("name");
					String deptName = model.get("deptName");
					String name = fullName + " [ " + deptName + " ]";
					model.set("shortName", Format.ellipse(name, 15));
					return model;
				}
				return null;
			}
		};
		listView2.setStore(staffStore);
		listView2.setDisplayProperty("shortName");
		listView2.setBorders(false);

		deptPanel.add(listView1);
		staffPanel.add(listView2);
		centerPanel.add(deptPanel, aboveData);
		centerPanel.add(staffPanel, belowData);
		add(centerPanel, centerData);
	}

	/**
	 * 显示树形结构
	 */
	private void createWest() {
		BorderLayoutData westData = new BorderLayoutData(LayoutRegion.WEST,
				225, 200, 250);
		westData.setMargins(new Margins(5, 0, 5, 5));
		VerticalPanel westPanel = new VerticalPanel();
		westPanel.setSpacing(8);
		westPanel.setBorders(true);
		westPanel.setLayout(new FormLayout());
		westPanel.setScrollMode(Scroll.AUTO);
		westPanel.setStyleAttribute("backgroundColor", "white");

		final TreePanel<ModelData> tree = new TreePanel<ModelData>(store);
		tree.setBorders(false);
		tree.setDisplayProperty("name");
		tree.setIconProvider(new TreeModelIconProvider());//显示图标
		tree.setCheckable(true);
		tree.setAutoLoad(true);
		tree.setCheckStyle(CheckCascade.NONE);

		tree.addCheckListener(new CheckChangedListener<ModelData>() {
			@Override
			public void checkChanged(CheckChangedEvent<ModelData> event) {
				deptStore.removeAll();
				staffStore.removeAll();
				List<ModelData> models = event.getCheckedSelection();
				for (int i = 0; i < models.size(); i++) {
					if (models.get(i).get("accountID") != null) {
						staffStore.add(models.get(i));
					} else {
						deptStore.add(models.get(i));
					}
				}
			}
		});

		StoreFilterField<ModelData> filter = new StoreFilterField<ModelData>() {
			@Override
			protected boolean doSelect(Store<ModelData> store,
					ModelData parent, ModelData record, String property,
					String filter) {
				String name = record.get("name");
				name = name.toLowerCase();
				if (name.indexOf(filter.toLowerCase()) != -1) {
					return true;
				}
				return false;
			}
		};
		filter.bind(store);

		ButtonBar buttonBar = new ButtonBar();
		buttonBar.add(new Button("Expand All",
				new SelectionListener<ButtonEvent>() {
					public void componentSelected(ButtonEvent ce) {
						tree.expandAll();
					}
				}));
		buttonBar.add(new Button("Collapse All",
				new SelectionListener<ButtonEvent>() {
					public void componentSelected(ButtonEvent ce) {
						tree.collapseAll();
					}
				}));
		westPanel
				.add(new Html(
						"<span style='background-color:#CE0000; color:white;'>&nbsp;&nbsp;选取部门继承至子部门&nbsp;&nbsp;</span>"));
		westPanel.add(filter);
		westPanel.add(buttonBar);
		westPanel.add(tree);

		add(westPanel, westData);
	}

	/**
	 * 取消按钮
	 * @return
	 */
	private Button getCancelButton() {
		Button button = new Button("取消");
		button.addSelectionListener(new SelectionListener<ButtonEvent>() {
			@Override
			public void componentSelected(ButtonEvent ce) {
				hide();
			}
		});
		return button;
	}

	private Button getConfirmButton(
			final Map<String, Field<String>> organization) {
		Button button = new Button("确定选取");
		button.addSelectionListener(new SelectionListener<ButtonEvent>() {
			@Override
			public void componentSelected(ButtonEvent ce) {
				StringBuffer sbID = new StringBuffer();//封装ID
				StringBuffer sbName = new StringBuffer();//封装Name
				for (ModelData model : deptStore.getModels()) {
					sbID.append(", " + (String) model.get("id"));
					sbName.append(", " + (String) model.get("name"));
				}
				for (ModelData model : staffStore.getModels()) {
					sbID.append(", " + (String) model.get("accountID"));
					sbName.append(", " + (String) model.get("name"));
				}
				String ID = sbID.toString();
				String name = sbName.toString();
				if (ID.length() > 1)
					ID = ID.substring(2);
				if (name.length() > 1) {
					name = name.substring(2);
				}
				//赋值
				organization.get("IDStr").setValue(ID);
				organization.get("nameStr").setValue(name);
				hide();
			}
		});
		return button;
	}

	/**
	 * 图标显示
	 * @author 汪恭泽
	 *
	 */
	public class TreeModelIconProvider implements ModelIconProvider<ModelData> {
		@Override
		public AbstractImagePrototype getIcon(ModelData model) {
			if (model.get("accountID") != null) {
				return IconHelper.createPath(GWT.getHostPageBaseURL()
						+ "images/icons/member.gif");
			} else {
				return IconHelper.createPath(GWT.getHostPageBaseURL()
						+ "images/icons/dept.gif");
			}
		}
	}

	public TreeStore<ModelData> getStore() {
		return store;
	}
}
